package cn.jhc.dao;

import cn.jhc.entity.Item;
import cn.jhc.entity.Order;
import cn.jhc.entity.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface ItemDAO {
    @Insert("insert into t_order_item(name,price,total_price,count,order_id,book_id) values(#{name},#{price},#{totalPrice},#{count},#{orderId},#{bookId})")
    @SelectKey(statement = "select last_insert_id()",keyProperty = "itemId",before = false,resultType = Integer.class)
    public int save(Item item);

    @Update({"<script>",
            "update t_order_item",
            "  <set>",
            "    <if test='name != null'>name=#{name},</if>",
            "    <if test='price != null'>price=#{price},</if>",
            "    <if test='totalPrice != null'>total_price=#{totalPrice},</if>",
            "    <if test='orderId != null'>order_id=#{orderId},</if>",
            "    <if test='bookId != null'>book_id=#{bookId},</if>",
            "  </set>",
            "where item_id=#{itemId}",
            "</script>"})
    public int update(Item item);

    @Delete("delete from t_order_item where item_id=#{itemId}")
    public int delete(Integer itemId);

    @Select("select * from t_order_item where item_id=#{itemId}")
    public Item queryOne(Integer itemId);

    @Select({"<script>",
            "select * from t_order_item",
            "  <where>",
            "    <if test='name != null'>name like '%${name}%'</if>",
            "    <if test='price != null'>price=#{price}</if>",
            "    <if test='totalPrice != null'>total_price=#{totalPrice}</if>",
            "    <if test='orderId != null'>order_id=#{orderId}</if>",
            "    <if test='bookId != null'>book_id=#{bookId}</if>",
            "  </where>",
            "</script>"})
    public List<Item> query(Item item);

    @Select("select * from t_order_item where book_id=#{bookId}")
    public List<Item> QueryByBook(Integer bookId);

}
